import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:syn_4s/core/extension/build_context_extension.dart';
import 'package:syn_4s/core/extension/int_extension.dart';
import 'package:syn_4s/core/utils/toast_utils.dart';

import '../../ui/shared/app_theme.dart';
import '../model/short_model.dart';

typedef ChoiceCallback<T> = void Function(T data);

class DialogUtils {
  static void loading(BuildContext context) {
    alertDialog(context, text: context.l10n.loading);
  }

  static void alertDialog(BuildContext context, {String? text}) {
    showDialog(
        builder: (context) {
          return AlertDialog(
            contentPadding: EdgeInsets.only(top: 16.px, bottom: 10.px),
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.px)),
            insetPadding: EdgeInsets.symmetric(horizontal: 110.px),
            backgroundColor: AppTheme.toastBgColors,
            content: SizedBox(
              height: 80.px,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const CircularProgressIndicator(
                    color: Colors.white,
                  ),
                  if (!(text == null || text.isEmpty))
                    Container(
                      margin: EdgeInsets.only(top: 10.px),
                      child: Text(
                        text,
                        style: const TextStyle(color: Colors.white),
                      ),
                    )
                ],
              ),
            ),
          );
        },
        context: context);
  }

  static void alertListDialog(BuildContext context, List<ShortModel> data,
      {String? title, String? selId, ChoiceCallback<ShortModel>? callback}) {
    if (selId != null && selId.isNotEmpty) {
      for (var item in data) {
        String code = item.code ?? "";
        if (code.isNotEmpty) {
          if (selId.contains(code)) {
            item.isSelect = true;
          } else {
            item.isSelect = false;
          }
        } else {
          item.isSelect = false;
        }
      }
    }
    showDialog(
        builder: (context) {
          return AlertDialog(
            contentPadding: EdgeInsets.only(top: 16.px, bottom: 10.px),
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.px)),
            content: SizedBox(
              width: 240.px,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    title ?? context.l10n.p_select,
                    style: const TextStyle(color: AppTheme.backgroundColors),
                  ),
                  SizedBox(
                    height: 10.px,
                  ),
                  Container(
                    height: 200.px,
                    child: ListView.builder(
                        itemCount: data.length,
                        itemBuilder: (ctx, index) {
                          return GestureDetector(
                            onTap: () {
                              closeDialog(context);
                              if (callback != null) {
                                callback(data[index]);
                              }
                            },
                            child: ListTile(
                              title: Text(data[index].name ?? ""),
                              trailing: (data[index].isSelect ?? false)
                                  ? const Icon(Icons.check_box,
                                      color: AppTheme.orangeColor)
                                  : const Icon(Icons.check_box_outline_blank),
                            ),
                          );
                        }),
                  )
                ],
              ),
            ),
          );
        },
        context: context);
  }

//
//   static void showAlertDialog(BuildContext context,
//       {String? content, Function? confirmCallback}) {
//     showDialog(
//         context: context,
//         builder: (ctx) {
//           return AlertDialog(
//             content: Text(content ?? ""),
//             actions: [
//               TextButton(
//                   onPressed: () {
//                     FocusScope.of(context).requestFocus(FocusNode());
//                     closeDialog(context);
//                   },
//                   child: Text(S.of(context).cancel)),
//               TextButton(
//                   onPressed: () {
//                     FocusScope.of(context).requestFocus(FocusNode());
//                     closeDialog(context);
//                     if (confirmCallback != null) {
//                       confirmCallback();
//                     }
//                   },
//                   child: Text(S.of(context).ok)),
//             ],
//           );
//         });
//   }
//
  static void alertMultiListDialog(BuildContext context, List<ShortModel> data,
      {String? title, String? selId, ChoiceCallback<List<ShortModel>>? callback}) {
    if (selId != null && selId.isNotEmpty) {
      for (var item in data) {
        String code = item.code ?? "";
        if (code.isNotEmpty) {
          if (selId.contains(code)) {
            item.isSelect = true;
          } else {
            item.isSelect = false;
          }
        } else {
          item.isSelect = false;
        }
      }
    } else {
      for (var item in data) {
        item.isSelect = true;
      }
    }
    final MultiController tagController = Get.put(MultiController());
    tagController.data.value = List.from(data.reversed);
    showDialog(
        builder: (context) {
          return AlertDialog(
            backgroundColor: AppTheme.backgroundColors,
            contentPadding: EdgeInsets.only(top: 16.px, bottom: 10.px),
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.px)),
            content: Container(
              padding: EdgeInsets.symmetric(horizontal: 16.px),
              width: 280.px,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Stack(
                    children: [
                      Center(
                        child: Text(
                          title ?? context.l10n.p_select,
                          style: TextStyle(fontSize: 18.px),
                        ),
                      ),
                      Positioned(
                        right: 0,
                        child: InkWell(
                          onTap: (){
                            closeDialog(context);
                          },
                          child: Icon(
                            color: AppTheme.textColor,
                            Icons.close,
                            size: 28.px,
                          ),
                        ),
                      )
                    ],
                  ),
                  SizedBox(
                    height: 16.px,
                  ),
                  SizedBox(
                    height: 250.px,
                    child: Obx(() => ListView.builder(
                        itemCount: tagController.data.length,
                        itemBuilder: (ctx, index) {
                          return GestureDetector(
                              onTap: () {
                                tagController.toggleSelect(index);
                              },
                              child: Container(
                                margin: EdgeInsets.only(bottom: 8.px),
                                child: Row(
                                  children: [
                                    getTagStatus( tagController.data[index].isSelect ?? false),
                                    SizedBox(
                                      width: 6.px,
                                    ),
                                    Text(
                                      tagController.data[index].name ?? "",
                                      style: TextStyle(
                                          color: Colors.black, fontSize: 16.px),
                                    )
                                  ],
                                ),
                              ));
                        })),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      getDialogButton(context.l10n.cancel,
                          margin: EdgeInsets.only(right: 8.px), onTap: () {
                        closeDialog(context);
                      }),
                      getDialogButton(context.l10n.confirm,
                          textColor: Colors.white,
                          bgColor: AppTheme.orangeColor, onTap: () {
                        if (callback != null) {
                          callback(tagController.data);
                        }
                      }),
                    ],
                  )
                ],
              ),
            ),
          );
        },
        context: context);
  }

  static Widget getTagStatus(bool isSelect) {
    if (isSelect) {
      return const Icon(
        Icons.check_box,
        color: AppTheme.orangeColor,
      );
    } else {
      return const Icon(Icons.check_box_outline_blank);
    }
  }

  static Widget getDialogButton(String text,
      {Color textColor = Colors.black87,
      Color bgColor = Colors.white,
      EdgeInsetsGeometry? margin,
      Function()? onTap}) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        alignment: Alignment.center,
        margin: margin,
        width: 72.px,
        height: 40.px,
        decoration: BoxDecoration(
            color: bgColor,
            borderRadius: BorderRadius.circular(6.px),
            border: Border.all(color: AppTheme.borderColor, width: 1.px)),
        child: Text(
          text,
          style: TextStyle(color: textColor, fontSize: 14.px),
        ),
      ),
    );
  }

//
//   static void showComboPicDialog(BuildContext context,
//       {ChoiceCallback<String?>? callback}) {
//     showModalBottomSheet(
//         context: context,
//         builder: (context) {
//           return SizedBox(
//             height: 250.px,
//             width: double.infinity,
//             child: Column(
//               children: [
//                 getPicButton(S.of(context).p_sel,
//                     textStyle: TextStyle(color: Colors.white, fontSize: 18.px),
//                     decoration: BoxDecoration(
//                         color: Colors.black,
//                         borderRadius: BorderRadius.vertical(
//                             top: Radius.circular(16.px)))),
//                 getPicButton(S.of(context).take_pic, onTap: () {
//                   ImagePicker imagePicker = ImagePicker();
//                   imagePicker
//                       .pickImage(source: ImageSource.camera)
//                       .then((value) {
//                     if (callback != null) {
//                       callback(value?.path);
//                     }
//                   });
//                 }),
//                 Container(
//                   height: 5.px,
//                   color: AppTheme.bgGreyColor,
//                 ),
//                 getPicButton(S.of(context).book_pic, onTap: () {
//                   ImagePicker imagePicker = ImagePicker();
//                   imagePicker
//                       .pickImage(source: ImageSource.gallery)
//                       .then((value) {
//                     if (callback != null) {
//                       callback(value?.path);
//                     }
//                   });
//                 }),
//                 Container(
//                   height: 5.px,
//                   color: AppTheme.bgGreyColor,
//                 ),
//                 getPicButton(S.of(context).cancel,
//                     textStyle: TextStyle(color: AppTheme.threeColor),
//                     onTap: () => closeDialog(context))
//               ],
//             ),
//           );
//         });
//   }
//
//   static Widget getPicButton(String title,
//       {TextStyle? textStyle, Decoration? decoration, Function()? onTap}) {
//     return GestureDetector(
//       onTap: onTap,
//       child: Container(
//           decoration: decoration,
//           alignment: Alignment.center,
//           width: double.infinity,
//           height: 60.px,
//           child: Text(
//             title,
//             style: textStyle,
//           )),
//     );
//   }
//
  static void closeDialog(BuildContext context) {
    Navigator.of(context).pop();
  }
}

class MultiController extends GetxController {
  var data = <ShortModel>[].obs;

  void toggleSelect(int index) {
    data[index].isSelect = !(data[index].isSelect ?? false);
    data.refresh();
  }
}
